package com.changgou.goods.controller;

import com.changgou.framework.entity.PageResult;
import com.changgou.framework.entity.Result;
import com.changgou.framework.entity.StatusCode;
import com.changgou.goods.service.AlbumService;
import com.haha.goods.entity.Album;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Yang
 * @date 2020/11/7
 * 相册的管理类
 */
@RestController
@RequestMapping("/album")
@CrossOrigin
public class AlbumController {
    @Autowired
    private AlbumService albumService;
    /***
     * Album分页条件搜索实现
     * @param album
     * @param page
     * @param size
     * @return
     */
    @PostMapping(value = "/search/{page}/{size}" )
    public Result findPage(@RequestBody(required = false) Album album, @PathVariable int page, @PathVariable  int size){
        //执行搜索
        PageResult pageInfo = albumService.findPage(album, page, size);
        return Result.ok(pageInfo);
    }
    /***
     * Album分页搜索实现
     * @param page:当前页
     * @param size:每页显示多少条
     * @return
     */
    @GetMapping(value = "/search/{page}/{size}" )
    public Result findPage(@PathVariable  int page, @PathVariable  int size){
        //分页查询
        PageResult pageInfo = albumService.findPage(null,page, size);
        return Result.ok(pageInfo);
    }
    /***
     * 多条件搜索品牌数据
     * @param album
     * @return
     */
    @PostMapping(value = "/search" )
    public Result findList(@RequestBody(required = false)  Album album){
        PageResult pageResult = albumService.findPage(album,1,Integer.MAX_VALUE);
        return Result.ok(pageResult.getRows());
    }

    /***
     * 根据ID删除品牌数据
     * @param id
     * @return
     */
    @DeleteMapping(value = "/{id}" )
    public Result delete(@PathVariable Long id){
        boolean b = albumService.removeById(id);
        return b?Result.ok(null):Result.error("删除失败");
    }
    /***
     * 修改Album数据
     * @param album
     * @param id
     * @return
     */
    @PutMapping(value="/{id}")
    public Result update(@RequestBody  Album album,@PathVariable Long id){
        //设置主键值
        album.setId(id);
        //修改数据
        boolean b = albumService.updateById(album);
        return b?Result.ok(null):Result.error("修改失败");
    }

    /***
     * 新增Album数据
     * @param album
     * @return
     */
    @PostMapping
    public Result add(@RequestBody   Album album){
        boolean b = albumService.save(album);
        return b?Result.ok(null):Result.error("新增失败");
    }
    /***
     * 根据ID查询Album数据
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result findById(@PathVariable Long id){
        //根据ID查询
        Album album = albumService.getById(id);
        boolean b = ObjectUtils.isEmpty(album);
        return !b?Result.ok(null):Result.error("查询失败");
    }
    /***
     * 查询Album全部数据
     * @return
     */
    @GetMapping
    public Result findAll(){
        List<Album> list = albumService.list();
        return Result.ok(list);
    }

}
